package com.shelpe.services.sustainer.step;

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.flow.FlowExecutionStatus;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.beans.factory.annotation.Autowired;

import com.shelpe.services.sustainer.repository.jpa.InprogressJobRepository;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AutoDriveStarterDecider implements JobExecutionDecider {

	private int threhold = 1;
	
	@Autowired
	private InprogressJobRepository inprogressJobRepository;
	
	public AutoDriveStarterDecider(){}
	public AutoDriveStarterDecider(int threhold){
		this.threhold = threhold;
	}
	
	
	@Override
	public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
		String jobName = jobExecution.getJobInstance().getJobName();
		int count = this.inprogressJobRepository.countByJobName(jobName);
		log.debug("inrpogress jobs: {} - {}", count, threhold);
		if(count > threhold){
			return new FlowExecutionStatus("nothing2do");
		}
		return FlowExecutionStatus.UNKNOWN;
	}

}
